package com.example.demo_job.controller;


import com.example.demo_job.config.MySessionListener;
import com.example.demo_job.entity.*;
import com.example.demo_job.pojo.User;
import com.example.demo_job.service.SeekerService;
import com.example.demo_job.utils.R;
import jakarta.servlet.ServletContext;
import jakarta.servlet.http.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/seeker")
//@CrossOrigin(origins = {"*"},methods = {RequestMethod.GET, RequestMethod.POST})
//@CrossOrigin
public class SeekerController extends HttpServlet {
    @Autowired
    private SeekerService seekerService;

    //登录地址：http://localhost:8080/hqdx/seeker/login
    @RequestMapping("/login")
    public R login(@RequestBody User user, HttpServletRequest request, HttpServletResponse response){
        //@RequestBody String username, @RequestBody String password,
        String susername=user.getUsername();
        String spassword=user.getPassword();
        Seeker seeker=seekerService.findByUsername(susername);
        if(seeker==null){
            //用户名不存在
            return R.error().message("用户名不存在");
        }
        else{
            //密码错误
            Seeker seekerLogin = seekerService.login(susername, spassword);
            if(seekerLogin==null) {
                return R.error().message("密码错误");
            }
            //登录成功
            String sname = seeker.getSname();
            Set<HttpSession> sessions = MySessionListener.getSessions();
            for (HttpSession session :sessions){
                if(sname.equals((String) session.getAttribute("currentUser"))){
                    return R.error().message("该用户已在其他地方登录");
                }
            }
            HttpSession session = request.getSession(true);
//            System.out.println(session+":"+session.getAttribute("currentUser"));
            Cookie cookie = new Cookie("JSESSIONID", session.getId());
            cookie.setPath("/");           // 全局生效
            cookie.setMaxAge(24 * 60 * 60); // 过期时间（秒，这里设 1 天）
            response.addCookie(cookie);

            System.out.println("set:"+session.getId());
            return R.ok().data("items",seekerLogin);
        }
    }
    //登出地址：http://localhost:8080/hqdx/seeker/loginout
    @RequestMapping("/loginout")
    public R loginout(String susername){
        Seeker seeker = seekerService.findByUsername(susername);
        MySessionListener.loginout(seeker.getSname());
        return R.ok().message("登出成功");
    }


    //注册地址：http://localhost:8080/hqdx/seeker/register
    @RequestMapping("/register")
    public R register( String susername,
                       String sname,
                       String spassword,
                       String sedu,
                       String sgender,
                       String sphone){
        Seeker seeker=seekerService.findByUsername(susername);
        if(seeker!=null) return R.error().message("用户名已存在");
        int sedu_new=Integer.valueOf(sedu);
        int register = seekerService.register(susername, sname, spassword, sedu_new, sgender, sphone);
        if(register==0) return R.error().message("注册失败");
        return R.ok().message("注册成功");
    }

    //搜索地址：http://localhost:8080/hqdx/seeker/searchforPosition
    @RequestMapping("/searchforPosition")
    public R searchforPosition(String keyword,String choice){
        if(choice.equals("岗位")){
            List<ComPosition> positionList1=seekerService.searchForPositionByPos(keyword);
            if(positionList1.size()==0) return R.error().message("集合为空");
            return R.ok().data("items",positionList1).data("choice2","岗位");
        }
        else{
            //公司
            List<OneComMulPosition> oneComMulPositionList=seekerService.searchForPositionByCom(keyword);
            if(oneComMulPositionList.size()==0) return R.error().message("空");
            return R.ok().data("items",oneComMulPositionList).data("choice2","公司");
        }
    }

    //推荐按照热度地址：http://localhost:8080/hqdx/seeker/findComPositionByHot
    @RequestMapping("/findComPositionByHot")
    public R findComPositionByHot(){
        List<Position> positionByHot = seekerService.findPositionByHot();
        if(positionByHot==null) return R.error().message("集合为空");
        List<ComPosition> compositionslist = seekerService.findComPositionByHot(positionByHot);
        return R.ok().data("items",compositionslist);
    }

    //修改密码地址：http://localhost:8080/hqdx/seeker/revisePassword
    @RequestMapping("/revisePassword")
    public R revisePassword(String susername, String newPassword){
        int sid=seekerService.findSeekerSidBySusername(susername);
        seekerService.revisePassword(sid,newPassword);
        Seeker seeker = seekerService.findSeekerBysid(sid);
        return  R.ok().message("修改成功").data("items",seeker);
    }

    //编写简历地址：http://localhost:8080/hqdx/seeker/reviseCV
    @RequestMapping("/reviseCV")
    public R reviseCV(String susername,String cv){
        String Tsinfo=seekerService.findSinfo(susername);
        if(Tsinfo==null) return R.error().message("失败");
        seekerService.write(Tsinfo,cv);
        return R.ok().message("修改成功");
    }

    //输出已投递地址：http://localhost:8080/hqdx/seeker/showApplyComPosition
    @RequestMapping("/showApplyComPosition")
    public R showApplyComPosition(String susername){
        int sid=seekerService.findSeekerSidBySusername(susername);
        List<ComPosition> comPositionList=seekerService.showApplyComPosition(sid);
        if(comPositionList.size()==0) return R.error().message("集合为空");
        return R.ok().data("items",comPositionList);
    }

    //返回个人简历：http://localhost:8080/hqdx/seeker/showSeekerCv
    @RequestMapping("/showSeekerCv")
    public R showSeekerCv(String sinfo){
        String Cv=seekerService.showSeekerCv(sinfo);
        if(Cv==null) return R.error().message("文本为空");
        return R.ok().data("items",Cv);
    }

    //返回是否申请过该职位地址：http://localhost:8080/hqdx/seeker/judgeApplyPosition
    @RequestMapping("/judgeApplyPosition")
    public R judgeApplyPosition(String susername, int pid){
        Recruit recruit=seekerService.judgeApplyPosition(susername,pid);
        if(recruit==null) return R.error().message("还未申请");
        return R.ok().message("你已经申请过");
    }

    //申请职位地址：http://localhost:8080/hqdx/seeker/applyPosition
    @RequestMapping("/applyPosition")
    public R applyPosition(String susername,int pid){
        Recruit recruit=seekerService.applyPosition(susername,pid);
        if(recruit==null) return R.ok().message("申请成功");
        return R.error().message("你已经申请过");
    }

    //取消申请职位地址：http://localhost:8080/hqdx/seeker/cancelApplyPosition
    @RequestMapping("/cancelApplyPosition")
    public R cancelApplyPosition( String susername,int pid){
        Recruit recruit = seekerService.cancelApplyPosition(susername,pid);
        if(recruit!=null) return R.ok().message("取消申请成功");
        return R.error().message("还未申请");
    }

    //返回公司列表地址：http://localhost:8080/hqdx/seeker/showAllCompanyPosition
    @RequestMapping("/showAllCompanyPosition")
    public R showAllCompanyPosition(HttpServletRequest request,HttpServletResponse response){
        HttpSession session = request.getSession(false);
        if(session!=null){
            System.out.println("get:"+session.getId());
        }
        List<Company> companyList=seekerService.showAllCompany();
        if(companyList==null) return R.error().message("空");
        List<OneComMulPosition> oneComMulPositionList=seekerService.showAllCompanyPosition(companyList);
        if(oneComMulPositionList.size()==0) return R.error().message("集合为空");
        return R.ok().data("items",oneComMulPositionList);
    }

    //岗位分类地址：http://localhost:8080/hqdx/seeker/PositionClassify
    @RequestMapping("/PositionClassify")
    public R PositionClassify(){

        List<Position> positionList=seekerService.showPosition();
        if(positionList==null) return R.error().message("空");
        List<OnePositionMulCompany> onePositionMulCompanyList=seekerService.PositionClassify(positionList);
        if(onePositionMulCompanyList.size()==0) return R.error().message("集合为空");
        return R.ok().data("items",onePositionMulCompanyList);
    }

    //无用接口
    @RequestMapping("/findPositionPath")
    public R findPositionPath(String pname,int cid){
        String path=seekerService.findPositionPath(cid,pname);
        if(path==null) return R.error().message("失败");
        return R.ok().data("items",path);
    }











}
